package com.xxxcompany.wordbook.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.xxxcompany.wordbook.entity.User;
import com.xxxcompany.wordbook.service.IUserService;
import com.xxxcompany.wordbook.service.UserServiceImpl;

@WebServlet("/registerchk")
public class UserRegisterCheckServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter pw = resp.getWriter();
		
		
		// 1. 获取用户参数
		User user = new User();
		try {
			BeanUtils.copyProperties(user, req.getParameterMap());
		} catch (IllegalAccessException | InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// 2. 调用service处理	
		IUserService userServcie = new UserServiceImpl();
		boolean result = userServcie.register(user);
		
		// 3. 跳转/请求转发页面
		if(result)
		{
//			resp.sendRedirect("login");
			
//			resp.setHeader("refresh", "5;url=http://www.baidu.com");
			
			req.getRequestDispatcher("/WEB-INF/register_ok.jsp").forward(req, resp);
		}
		else
		{
			resp.sendRedirect("register");
		}
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}